package top.birdhk.TestAPI.transorm;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import top.birdhk.TestAPI.beans.SensorReading;

/**
 * 滚动聚合
 */
public class Transform_RollingAggregation {

    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> inputSream = env.readTextFile("E:\\flink\\wordcount\\src\\main\\resources\\sensor.txt");

        env.setParallelism(1);

        // 转换成SensorReading
//        inputSream.map(new MapFunction<String, SensorReading>() {
//            @Override
//            public SensorReading map(String s) throws Exception {
//                String[] split = s.split(",");
//                return new SensorReading(split[0],new Long(split[1]),new Double(split[2]));
//            }
//        });
        SingleOutputStreamOperator<SensorReading> dataStream = inputSream.map(line -> {
            String[] split = line.split(",");
            return new SensorReading(split[0], new Long(split[1]), new Double(split[2]));
        });


        // 分组
//        KeyedStream<SensorReading, Tuple> id = dataStream.keyBy("id");
        KeyedStream<SensorReading, String> keyedStream = dataStream.keyBy(SensorReading::getId);


        // 滚动聚合,取当前的最大值
        SingleOutputStreamOperator<SensorReading> max = keyedStream.maxBy("temperature");

        max.print();

        env.execute();


    }


}
